package com.goods.business.controller;


import com.goods.business.entities.Health;
import com.goods.business.service.HealthService;
import com.goods.business.utils.HttpUtils;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/business/health")
public class BizHealthController {

    @Autowired
    private HealthService healthService;


    @GetMapping("isReport")
    public ResponseBean isReport(@RequestHeader("Authorization") String token) {
        // 1. 从token中获取用户名
        String username = JWTUtils.getUsername(token);
        // 2. 远程调用接口 system/user/getUserId 获取用户id
        Long userId = HttpUtils.getUserIdByUserName(token, username);
        // 3. 根据用户id去查询今日签到记录
        Health record = healthService.findHealthyCheckInRecord(userId);
        if (Objects.isNull(record)) {
            return ResponseBean.success(null);
        }
        return ResponseBean.success(record);
    }

    @PostMapping("report")
    public ResponseBean report(@RequestHeader("Authorization") String token, @RequestBody HealthVO healthVO) {
        // 1. 从token中获取用户名
        String username = JWTUtils.getUsername(token);
        // 2. 远程调用接口 system/user/getUserId 获取用户id
        Long userId = HttpUtils.getUserIdByUserName(token, username);
        healthVO.setUserId(userId);
        // 3. 签到
        healthService.checkIn(healthVO);
        return ResponseBean.success();
    }

    // history
    @GetMapping("history")
    public ResponseBean<PageVO<HealthVO>> history(
            @RequestHeader("Authorization") String token,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "4") Integer pageSize) {
        // 1. 从token中获取用户名
        String username = JWTUtils.getUsername(token);
        // 2. 远程调用接口 system/user/getUserId 获取用户id
        Long userId = HttpUtils.getUserIdByUserName(token, username);
        PageVO<HealthVO> list = healthService.findAllRecord(pageNum, pageSize, userId);
        return ResponseBean.success(list);

    }


}

